package com.hddf.project.action;

import java.util.List;

import com.hddf.project.base.BaseAction;
import com.hddf.project.pojos.FirstCategory;
import com.hddf.project.pojos.SecondCategory;
import com.hddf.project.pojos.ThirdCategory;
import com.hddf.project.pojos.User;
import com.hddf.project.service.CategoryService;
import com.hddf.project.service.LogsService;
import com.hddf.project.util.O;
import com.hddf.project.util.Stct;

public class CategoryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<FirstCategory> fcList;
	private List<SecondCategory> scList;
	private List<ThirdCategory> tcList;
	private Integer f_id;// 一级ID
	private Integer s_id;// 二级ID
	private Integer t_id;// 三级ID
	private String ids;// 批量处理ID字符串
	private String name;
	private Integer sort;
	private FirstCategory fc;
	private SecondCategory sc;
	private ThirdCategory tc;
	private Integer isSort;// 添加删除时是否需要排序

	/**
	 * 跳转到一级类展示首页
	 * 
	 * @return
	 */
	public String jumpFirstCategory() {
		setSuccessPath("/page/category/category_first.html");
		return SUCCESS;
	}

	/**
	 * 跳转到二级分类页面
	 */
	public String jumpSecondCategory() {
		setSuccessPath("/page/category/category_second.html");
		return SUCCESS;
	}

	/**
	 * 跳转到二级分类页面
	 */
	public String jumpThirdCategory() {
		setSuccessPath("/page/category/category_third.html");
		return SUCCESS;
	}

	/**
	 * 添加第一类别
	 */
	public void addFirstCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort) {
					fc = cs.addFirstCategory(name, sort, isSort);
					log(1, "add first category", fc.getFirst_category_id(), fc.getFirst_category_name());

					ls = LogsService.getInService();
					ls.addLog(user, "添加一级分类，ID为：" + fc.getFirst_category_id() + "，名称为：" + name, (short) 1);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 添加第二类别
	 */
	public void addSecondCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort && 0 != f_id) {
					sc = cs.addSecondCategory(name, sort, f_id, isSort);
					log(1, "add second category", sc.getSecond_category_id(), sc.getSecond_category_name());

					ls = LogsService.getInService();
					ls.addLog(user, "添加二级分类，ID为：" + sc.getSecond_category_id() + "，名称为：" + name, (short) 1);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 添加第三类别
	 */
	public void addThirdCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort && 0 != s_id) {
					tc = cs.addThirdCategory(name, sort, s_id, isSort);
					log(1, "add third category", tc.getThird_category_id(), tc.getThird_category_name());

					ls = LogsService.getInService();
					ls.addLog(user, "添加三级分类，ID为：" + tc.getThird_category_id() + "，名称为：" + name, (short) 1);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改一级分类
	 */
	public void updateFirstCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort && 0 != f_id) {
					cs.updateFirstCategory(f_id, name, sort, isSort);
					log(1, "update first category", f_id, name);

					ls = LogsService.getInService();
					ls.addLog(user, "修改一级分类，ID为：" + f_id + "，名称为：" + name, (short) 2);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改二级分类
	 */
	public void updateSecondCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort && 0 != s_id) {
					cs.updateSecondCategory(s_id, name, sort, isSort);
					log(1, "update second category", s_id, name);

					ls = LogsService.getInService();
					ls.addLog(user, "修改二级分类，ID为：" + s_id + "，名称为：" + name, (short) 2);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改三级分类
	 */
	public void updateThirdCategory() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				if (O.ckSt(name) && 0 != sort && 0 != isSort && 0 != t_id) {
					cs.updateThirdCategory(t_id, name, sort, isSort);
					log(1, "update third category", t_id, name);
				} else {
					state = "error";
					msg = "请填写正确内容";
				}

				ls = LogsService.getInService();
				ls.addLog(user, "修改三级分类，ID为：" + t_id + "，名称为：" + name, (short) 2);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除一级分类
	 */
	public void deleteFristCategory() {
		int result = 0;
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				result = cs.deleteFirstCategoryAndOthers(f_id, isSort);
				switch (result) {
				case Stct.MESSAGE_SUCCESS:
					log(1, "delete first category id", f_id);
					break;
				case Stct.ERROR:
					state = "error";
					msg = "删除失败";
					break;
				case Stct.MESSAGE_USED:
					state = "error";
					msg = "该分类处于使用中，无法删除！";
					break;
				}

				ls = LogsService.getInService();
				ls.addLog(user, "删除一级分类，ID为：" + f_id, (short) 3);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "删除失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除二级分类
	 */
	public void deleteSecondCategory() {
		int result = 0;
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				result = cs.deleteSecondCategoryAndOthers(s_id, isSort);
				switch (result) {
				case Stct.MESSAGE_SUCCESS:
					log(1, "delete second category id", s_id);
					break;
				case Stct.ERROR:
					state = "error";
					msg = "删除失败";
					break;
				case Stct.MESSAGE_USED:
					state = "error";
					msg = "该分类处于使用中，无法删除！";
					break;
				}
				ls = LogsService.getInService();
				ls.addLog(user, "删除二级分类，ID为：" + s_id, (short) 3);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "删除失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除三级分类
	 */
	public void deleteThirdCategory() {
		int result = 0;
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CategoryService cs = CategoryService.getInService();
				result = cs.deleteThirdCategoryAndOthers(t_id, isSort);
				switch (result) {
				case Stct.MESSAGE_SUCCESS:
					log(1, "delete thrid category id", t_id);
					break;
				case Stct.ERROR:
					state = "error";
					msg = "删除失败";
					break;
				case Stct.MESSAGE_USED:
					state = "error";
					msg = "该分类处于使用中，无法删除！";
					break;
				}

				ls = LogsService.getInService();
				ls.addLog(user, "删除三级分类，ID为：" + t_id, (short) 3);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "删除失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取一级分类信息
	 */
	public void getFirstCategoryById() {
		try {
			CategoryService cs = CategoryService.getInService();
			fc = cs.getFirstCategory(f_id);
			msg = fc;
			log(2, "get first category by first id", f_id);
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取二级分类信息
	 */
	public void getSecondCategoryById() {
		try {
			CategoryService cs = CategoryService.getInService();
			sc = cs.getSecondCategory(s_id);
			msg = sc;
			log(2, "get second category by second id", s_id);
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取三级分类信息
	 */
	public void getThirdCategoryById() {
		try {
			CategoryService cs = CategoryService.getInService();
			tc = cs.getThirdCategory(t_id);
			msg = tc;
			log(2, "get third category by third id", t_id);
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取全部一级类别
	 */
	public void getAllFirstCategory() {
		try {
			CategoryService cs = CategoryService.getInService();
			fcList = cs.getAllFirstCategory();
			msg = fcList;
			log(2, "get all first category");
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据一级类别ID获取二级子分类
	 * 
	 * @param f_id
	 * @return
	 */
	public void getSecondCategoryByFirstCategoryId() {
		try {
			CategoryService cs = CategoryService.getInService();
			scList = cs.getSecondCategoryByFirstCategoryId(f_id);
			msg = scList;
			log(2, "get second category by first category id", f_id);
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据二级类别ID获取三级子分类
	 * 
	 * @param f_id
	 * @return
	 */
	public void getThirdCategoryBySecondCategoryId() {
		try {
			CategoryService cs = CategoryService.getInService();
			tcList = cs.getThirdCategoryBySecondCategoryId(s_id);
			msg = tcList;
			log(2, "get third category by second category id", s_id);
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取全部类别，并保存分类之间的关系
	 */
	public void getAllCategory() {
		try {
			CategoryService cs = CategoryService.getInService();
			fcList = cs.getListFirstCategoryAndOthers();
			msg = fcList;
			log(2, "get all category");
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取全部一级类别和类别的项目数量
	 */
	public void getAllFirstCategoryAndCount() {
		try {
			CategoryService cs = CategoryService.getInService();
			// fcList = cs.getAllFirstCategoryAndCount();
			scList = cs.listIndexCategory();
			msg = scList;
			log(2, "get all first category count");
		} catch (Exception e) {
			msg = "系统异常";
			state = "error";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	public List<FirstCategory> getFcList() {
		return fcList;
	}

	public void setFcList(List<FirstCategory> fcList) {
		this.fcList = fcList;
	}

	public List<SecondCategory> getScList() {
		return scList;
	}

	public void setScList(List<SecondCategory> scList) {
		this.scList = scList;
	}

	public List<ThirdCategory> getTcList() {
		return tcList;
	}

	public void setTcList(List<ThirdCategory> tcList) {
		this.tcList = tcList;
	}

	public Integer getF_id() {
		return f_id;
	}

	public void setF_id(Integer f_id) {
		this.f_id = f_id;
	}

	public Integer getS_id() {
		return s_id;
	}

	public void setS_id(Integer s_id) {
		this.s_id = s_id;
	}

	public Integer getT_id() {
		return t_id;
	}

	public void setT_id(Integer t_id) {
		this.t_id = t_id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public FirstCategory getFc() {
		return fc;
	}

	public void setFc(FirstCategory fc) {
		this.fc = fc;
	}

	public SecondCategory getSc() {
		return sc;
	}

	public void setSc(SecondCategory sc) {
		this.sc = sc;
	}

	public ThirdCategory getTc() {
		return tc;
	}

	public void setTc(ThirdCategory tc) {
		this.tc = tc;
	}

	public Integer getIsSort() {
		return isSort;
	}

	public void setIsSort(Integer isSort) {
		this.isSort = isSort;
	}

}